package org.example.mall.admin.configuration.auth.check;

import lombok.extern.slf4j.Slf4j;
import org.example.mall.admin.configuration.auth.code.ScenesConst;
import org.example.mall.admin.constant.auth.AuthTypeConst;
import org.example.mall.admin.util.SecurityUtil;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.stereotype.Component;

/**
 * 用户名密码登录校验
 *
 * @author hzq
 * @date 2021/12/16 13:51
 */
@Slf4j
@Component
public class UsernameCheckAuthHandler implements CheckAuthHandler {

    @Override
    public Object check(String scenes, String principal, String credentials) {

        boolean matches = SecurityUtil.PASSWORD_ENCODER.matches(principal, credentials);
        if (!matches) {
            throw new BadCredentialsException("用户名或密码错误");
        }

        return null;
    }

    @Override
    public boolean supportType(String checkType) {
        return AuthTypeConst.USERNAME.equals(checkType);
    }

    @Override
    public boolean supportScenes(String scenes) {
        return ScenesConst.LOGIN.equals(scenes);
    }
}
